<template>
  <!-- 关键生产设备 -->
  <div>
    <curd-title v-if="infoFlg" :title="'关键生产设备'" />
    <avue-crud
      :table-loading="equipmentLoading"
      :option="equipmentOption"
      :data="equipmentList"
      @on-load="onEquipmentLoad"
      @row-save="rowEquipmentSave"
      @row-update="rowEquipmentUpdate"
      @refresh-change="refreshEquipmentChange"
      @row-del="rowEquipmentDel"
      :row-class-name="delRowClassName"

    >
      <template slot="equipmentName" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.equipmentNameFlag === '1'">{{ scope.row.equipmentName }}</del>
          {{ scope.row.equipmentNameA }}
        </div>
        <span v-else>{{ scope.row.equipmentName }}</span>
      </template>
      <template slot="brand" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.brandFlag === '1'">{{ scope.row.brand }}</del>
          {{ scope.row.brandA }}
        </div>
        <span v-else>{{ scope.row.brand }}</span>
      </template>
      <template slot="quantity" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.quantityFlag === '1'">{{ scope.row.quantity }}</del>
          {{ scope.row.quantityA }}
        </div>
        <span v-else>{{ scope.row.quantity }}</span>
      </template>
      <template slot="beginDate" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.beginDateFlag === '1'">{{ scope.row.beginDate }}</del>
          {{ scope.row.beginDateA }}
        </div>
        <span v-else>{{ scope.row.beginDate }}</span>
      </template>
      <template slot="advantageDescription" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.advantageDescriptionFlag === '1'">{{ scope.row.advantageDescription }}</del>
          {{ scope.row.advantageDescriptionA }}
        </div>
        <span v-else>{{ scope.row.advantageDescription }}</span>
      </template>
    </avue-crud>
  </div>
</template>

<script>
import CurdTitle from '@/components/base/title'
import { equipmentOption } from '@/option/tenant/supplier/equipment-info'
import { getEquipmentList, removeEquipment, addEquipment, updateEquipment } from '@/api/supplier/equipment'
import { getStore } from '@/util/store'
import delRowClassName from '@/views/plugin/workflow/mixins/info'

export default {
  name: 'EquipmentInfo',
  components: { CurdTitle },
  inject: {
    type: {
      default: 'view'
    },
    infoFlg: {
      default: true
    },
    slotFlg: {
      default: false
    }
  },
  mixins: [delRowClassName],
  data() {
    return {
      supplierId: getStore({ name: 'supplierId' }),
      equipmentList: [],
      equipmentLoading: false,
      equipmentOption: equipmentOption.call(this)
    }
  },
  methods: {
    onEquipmentLoad() {
      this.infoFlg
        ? ((this.equipmentLoading = true),
          getEquipmentList({ supplierId: this.supplierId }).then(res => {
            this.equipmentList = res.data.data
            this.equipmentLoading = false
          }))
        : ''
    },
    refreshEquipmentChange() {
      this.onEquipmentLoad()
    },
    rowEquipmentSave(row, done, loading) {
      addEquipment({ ...row, supplierId: this.supplierId }).then(
        () => {
          this.onEquipmentLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowEquipmentUpdate(row, index, done, loading) {
      updateEquipment(row).then(
        () => {
          this.onEquipmentLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowEquipmentDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return removeEquipment(row.id)
        })
        .then(() => {
          this.onEquipmentLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
    }
  }
}
</script>

<style></style>
